package com.liziba.springcloud.example.springclouduserservice;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @auther LiZiBa
 * @date 2020/8/3 0:28
 * @description:
 **/

@RestController
public class UserService {

    @Autowired
    private RestTemplate restTemplate;

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){

        return  new RestTemplate();
    }

//    @Autowired
//    LoadBalancerClient loadBalancerClient;

    @GetMapping("/user/{id}")
    public String findById(@PathVariable("id") int id){

        // TODO
        // 查询订单服务 HttpClient  RestTemplete JDK/URLHttpConnector

//        ServiceInstance serviceInstance = loadBalancerClient.choose("server-cloud-order-service");
//        String url = String.format("http://%s:%d/orders",serviceInstance.getHost(), serviceInstance.getPort());

        return restTemplate.getForObject("http://server-cloud-order-service/orders", String.class);
    }

}
